﻿
#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
#endregion

public partial class AddUtilityGroup : System.Web.UI.UserControl
{
    UserManager _utilityGroupManager = new UserManager();
    
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack0.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack0.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnClear.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnClear.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSave.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSave.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        if (!IsPostBack)
        {
            if (Session["UserManager_UtilityGroupEdit"] == "Edit")
            {
                btnSave.Text = "Update";
                LoadData();
            }
        }
    }

    protected void imbDate_Click(object sender, ImageClickEventArgs e)
    {
       // calDate.Visible = true;
    }

    protected void calDate_SelectionChanged(object sender, EventArgs e)
    {
       // txtDate.Text = calDate.SelectedDate.ToShortDateString();
       // calDate.Visible = false;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucSearchUtilityGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        Clear();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        //if (txtName.Text == string.Empty)
        //{
        //    Response.Write("<script>alert('Enter Utility Group Name')</script>");
        //    return;
        //}
        //if (txtCreatedBy.Text == string.Empty)
        //{
        //    Response.Write("<script>alert('Enter the Creator Name')</script>");
        //    return;
        //}

        if (Session["UserManager_UtilityGroupEdit"] == "Edit")
            Edit();
        else
            Add();
        Clear();
    }

    #endregion

    #region Methods
    void Add()
    {
        try
        {

            Utility utilityGroup = new Utility();

            utilityGroup.GroupName = txtName.Text;
            utilityGroup.GroupDescription = txtDescription.Text;
            utilityGroup.CreatedBy = txtCreatedBy.Text;
            utilityGroup.DateOfCreation = txtDate.Text;

            if (!_utilityGroupManager.AddUtilityGroup(utilityGroup))
            {
                Response.Write("<script>alert('Utility Group Already Exist')</script>");
            }
            else
                Response.Write("<script>alert('Added Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Utility Group Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Edit()
    {
        try
        {
            Utility utilityGroupEdit = new Utility();
            utilityGroupEdit.GroupID = Session["UserManager_UtilityGroupID"].ToString();
            utilityGroupEdit.GroupName = txtName.Text;
            utilityGroupEdit.GroupDescription = txtDescription.Text;
            utilityGroupEdit.CreatedBy = txtCreatedBy.Text;
            utilityGroupEdit.DateOfCreation = txtDate.Text;

            if (_utilityGroupManager.EditUtilityGroup(utilityGroupEdit))
                Response.Write("<script>alert('Modified Successfully!')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Utility Group Modification Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void LoadData()
    {
        string condition = "UG_GroupID = " + Session["UserManager_UtilityGroupID"] + " ";
        DataSet dsUser = _utilityGroupManager.GetData("UtilityGroup", condition);
        string dateofCreation = dsUser.Tables[0].Rows[0]["UG_CreatedDate"].ToString();

        txtName.Text = dsUser.Tables[0].Rows[0]["UG_GroupName"].ToString();
        txtDescription.Text = dsUser.Tables[0].Rows[0]["UG_Description"].ToString();
        txtCreatedBy.Text = dsUser.Tables[0].Rows[0]["UG_CreatedBy"].ToString();
        if (dsUser.Tables[0].Rows[0]["UG_CreatedDate"]!= DBNull.Value )
        txtDate.Text = dateofCreation.Substring(0, dateofCreation.IndexOf(" "));

    }

    void Clear()
    {
        txtDate.Text = string.Empty;
        txtName.Text = string.Empty;
        txtDescription.Text = string.Empty;
        txtCreatedBy.Text = string.Empty;
        txtDate.Text = string.Empty;
    }
    #endregion
}
